import request from "@/utils/request";
import {
  rootsUrl
} from "@/utils";
import {
  getToken
} from "@/utils/auth";
import {
  wechat
} from "@/config/ThirdPartyLogin";

import Qs from "qs";

/**
 * 通过用户名密码登录
 * @param {String} userId
 * @param {String} password
 */
export function loginByUsername(userId, password) {
  let data = {
    userId,
    password
  };
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/login.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
}

/**
 * 判断是否登录  并返回账号ID
 * @param {String} token
 */
export function getLoginUser(token) {
  let data = {
    token: token
  };
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/getLoginUser.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
}

/**
 * 用户注册
 * @param {String} userId
 * @param {String} password
 * @param {String} mobile
 */
export function register(userId, password, mobile) {
  let data = {
    userId,
    password,
    mobile
  };
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/register.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
}

/**
 * 用户信息查询
 */
export function getUserInfo() {
  let token = getToken();
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/userInfo.shtml`,
    method: "get",
    params: {
      token,
      random: Math.random()
    }
  });
}

/**
 * 获取用户头像
 */
export function getUserAvatar(userId) {
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/showUserHead.shtml`,
    method: "get",
    params: {
      userId
    }
  });
}

/**
 * 获取证件类型
 */
export function getCardType() {
  return request({
    url: `${rootsUrl[0]}/museum/dictionary/listDictionary.shtml`,
    method: "get",
    params: {
      type: 2
    }
  });
}

/**
 * 用户点赞次数查询
 */
export function getCountThumbsupByUser() {
  let token = getToken();
  return request({
    url: `${rootsUrl[0]}/museum/thumbsup/countThumbsupByUser.shtml`,
    method: "get",
    params: {
      token
    }
  });
}

/**
 * 获取用户预约活动总数
 * @param {*} userId
 */
export function getCountAppointActByUser(userId) {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/lecture/getLecturePersonNum.shtml`,
    method: "get",
    params: {
      token,
      mid: userId
    }
  });
}

/**
 * 用户点赞记录
 */
export function getRecordThumbsupByUser() {
  let token = getToken();
  return request({
    url: `${rootsUrl[0]}/museum/thumbsup/listThumbsupByUser.shtml`,
    method: "get",
    params: {
      token
    }
  });
}

/**
 * 获取我的创意作品
 * @param {Number} userId
 */
export function getMyArt(userId) {
  let token = getToken();
  console.log("userId", userId);
  return request({
    url: `${rootsUrl[3]}/cbculture/design/getDesignList.shtml`,
    method: "get",
    params: {
      token,
      mid: userId
    }
  });
}

/**
 * 获取我的展览创意作品
 * @param {Number} userId
 */
export function getMyExhibition(userId) {
  let token = getToken();
  console.log("userId", userId);
  return request({
    url: `${rootsUrl[0]}/museum/design/getDesignList.shtml`,
    method: "get",
    params: {
      token,
      mid: userId
    }
  });
}

/**
 * 参观次数查询
 */
export function getVistCount() {
  let token = getToken();
  return request({
    url: `${rootsUrl[2]}/cbticket/orderList/getVistTimes.shtml`,
    method: "get",
    params: {
      token
    }
  });
}

/**
 * 修改用户信息
 * @param {Object} data
 */
export function updateUserInfo(data) {
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/updateUser.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
}

/**
 * 修改用户头像
 * @param {Object} data
 */
export function editAvatar(userHead) {
  let data = {
    token: getToken(),
    userHead: userHead,
    fileName: "用户头像.jpeg"
  };
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/updateUserHead.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
}

/**
 * 修改密码
 * @param {Object} data
 */
export function updatePassword(oldPass, newPass) {
  let token = getToken();
  let data = {
    oldPassword: oldPass,
    newPassword: newPass,
    token
  };
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/updatePassword.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
}

export function logout() {
  return request({
    url: "/login/logout",
    method: "post"
  });
}

/**
 * 获取验证码
 * @param {*} userId 用户账号
 */
export function getQrcode(userId) {
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/sendResetPwdCode.shtml`,
    method: "get",
    params: {
      userId
    }
  });
}

export function resetPassword(userId, code, password) {
  let data = {
    userId,
    code,
    password
  };
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/resetPassword.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
}

export function loginByWechat(code) {
  let data = {
    code
  };
  return request({
    url: `${rootsUrl[0]}/museum/gatewayUser/wxLogin.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
}
